package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseAttribute;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;

	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	@Autowired
	private BaseAttributeValueMapper baseAttributeValueMapper;

	@Override
	public List<JSONObject> getBaseCategoryList() {
		//	创建集合List<JSONObject>
		List<JSONObject> list = new ArrayList<>();
		//	数据封装--
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
		//	获取一级分类数据 - 按照一级分类Id 进行分组得到一个 map 集合
		// 	key = category1Id	value = List<BaseCategoryView>
		//	BaseCategoryView::getCategory1Id 方法引用 获取一级分类Id  分组条件：group by category1_id;
		Map<Long, List<BaseCategoryView>> map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		//	循环遍历map集合
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = map.entrySet().iterator();
		//	遍历 判断是否有一下个数据
		while (iterator.hasNext()) {
			//	从迭代器中获取数据
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			//	获取key数据
			Long category1Id = entry.getKey();
			//	获取value数据
			List<BaseCategoryView> baseCategoryViewList1 = entry.getValue();
			//	创建一个一级分类对象
			JSONObject category1 = new JSONObject();
			category1.put("categoryId", category1Id);
			category1.put("categoryName", baseCategoryViewList1.get(0).getCategory1Name());

			//	获取二级分类集合 key = category2Id value=list<BaseCategoryView>
			Map<Long, List<BaseCategoryView>> map2 = baseCategoryViewList1.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			//	创建一个二级分类集合
			List<JSONObject> category2List = new ArrayList<>();
			//	循环遍历map2
			Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator1 = map2.entrySet().iterator();
			while (iterator1.hasNext()) {
				Map.Entry<Long, List<BaseCategoryView>> entry1 = iterator1.next();
				//	获取数据
				Long category2Id = entry1.getKey();
				List<BaseCategoryView> baseCategoryViewList2 = entry1.getValue();
				//	创建一个二级分类对象
				JSONObject category2 = new JSONObject();
				category2.put("categoryId", category2Id);
				category2.put("categoryName", baseCategoryViewList2.get(0).getCategory2Name());
				List<JSONObject> arrayList = baseCategoryViewList2.stream().map(baseCategoryView -> {
					//	创建一个三级分类对象
					JSONObject category3 = new JSONObject();
					category3.put("categoryId", baseCategoryView.getCategory3Id());
					category3.put("categoryName", baseCategoryView.getCategory3Name());
					return category3;
				}).collect(Collectors.toList());
				//  保存数据
				category2.put("categoryChild", arrayList);
				//	添加数据
				category2List.add(category2);
			}
			//  二级分类集合添加到一级分类中.
			category1.put("categoryChild", category2List);
			//  将一级分类集合添加到最外层集合。
			list.add(category1);
		}
		//  返回数据
		return list;
	}

	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		// 根据一级非分类Id获取属性标签数据 sql
		return baseAttributeMapper.selectAttribute(category1Id);
	}
}
